import fs from 'fs'
import ContentWindow from '../src/UI/Windows/Common/Content'
import { RP_BASE_PATH } from '../src/constants'
import PackLinker from '../src/Utilities/LinkPacks'

export default class LinkRPWindow extends ContentWindow {
	private selected_rp: string

	constructor(bp_name: string) {
		const PROJECTS = (
			fs.readdirSync(RP_BASE_PATH, {
				withFileTypes: true,
			})
		)
			.filter(dirent => dirent.isDirectory())
			.map(dirent => dirent.name)

		super({
			display_name: 'Link Project To Resource Pack',
			options: {
				is_persistent: false,
				height: 170,
			},
			content: [
				{
					text:
						'Create a link between the currently selected behavior pack and one of your resource packs.\n\n',
				},
				{
					type: 'select',
					input: PROJECTS[0],
					color: 'primary',
					options: PROJECTS,

					action: (val: string) => {
						this.selected_rp = val
					},
				},
			],
			actions: [
				{
					type: 'space',
				},
				{
					type: 'button',
					text: 'Link!',
					color: 'primary',
					is_rounded: false,
					action: () => {
						this.close()
						setTimeout(
							() => PackLinker.link(bp_name, this.selected_rp),
							300
						)
					},
				},
			],
		})

		this.selected_rp = PROJECTS[0]
	}
}
